package com.zzyl.chinamobileai.controller;

import com.zzyl.chinamobileai.entity.vo.MessageVO;
import com.zzyl.chinamobileai.service.IChatHistoryService;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ai/history")
public class ChatHistoryController {
    @Autowired
    private ChatMemoryRepository chatMemoryRepository;

    @Autowired
    private ChatMemory chatMemory;

    @Autowired
    private IChatHistoryService chatHistoryService;


    @GetMapping("{type}")
    public List<String> getHistoryList(@PathVariable String type){
        //干什么事？
        //查找会话历史表里所有类型为chat的
        return chatHistoryService.getChatIds(type,null);
    }

    //这里是查找出这个会话的所有记录，这样前端才能展示
    @GetMapping("/{type}/{chatId}")
    public List<MessageVO> getChatMessageList(@PathVariable String type, @PathVariable String chatId){
        //List<Message> messages = chatMemoryRepository.findByConversationId(chatId);
        List<Message> messageList = chatMemory.get(chatId);
        return messageList.stream().map(MessageVO::new).toList();
    }
}
